/**外包企业门户网站,生成时间:2017-04-19 09:55:35**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.xbdun.dao.bmp.entity.XbdunDepartDo;

/**
 * 动态XbdunDepartDo的SQL语句提供者
 * 部门
 * @author zhanggt
 * @2017-04-19 09:55:35
 */
public class DepartSqlProvider {

	//日志
	Log log = LogFactory.getLog(DepartSqlProvider.class);
	
	//添加部门
	public String addXbdunDepartSql(Map<String,Object> params){
		XbdunDepartDo xbdunMenuDo = (XbdunDepartDo)params.get("a");
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("INSERT INTO xbdun_depart(id,orgCode,code,sort,departName,remark,createTime,optionUser,version)");
		sbSql.append("VALUES(#{a.id},#{a.orgCode},(");
		//设置菜单最大值
		if(xbdunMenuDo.getCode()<=0){
			sbSql.append("SELECT IFNULL(MAX(code),99999)+1 FROM xbdun_depart b WHERE b.orgCode = #{a.orgCode} AND  b.code LIKE '______'");
		}else{
			sbSql.append("select CASE LENGTH(c.maxnum) WHEN LENGTH(#{a.code}) THEN CONCAT(#{a.code},c.maxnum) WHEN 6 THEN CONCAT(#{a.code},c.maxnum) ELSE c.maxnum end  FROM (SELECT (IFNULL(MAX(code),99999)+1) maxnum FROM xbdun_depart b WHERE b.orgCode = #{a.orgCode} AND b.code LIKE CONCAT(#{a.code},'______')) c");
		}
		sbSql.append("),#{a.sort},#{a.departName},#{a.remark},now(),#{a.optionUser},now())");
		
		String sqlContent = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}
	
	/**
	 * 动态sql更新对象
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateDynXbdunDepartSql(Map<String,Object> map){
		
		StringBuilder sbSql = new StringBuilder();
		Map<String,Object> params = (Map<String, Object>) map.get("param");
		String _where = constWhere(map);
		boolean bl = false;
		
		Assert.isTrue(StringUtils.isNotEmpty(_where),"动态更新操作，条件Where不存在");
		
		//移除Where条件
		sbSql.append("UPDATE xbdun_depart SET ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = params.entrySet().iterator();
			while(iparam.hasNext()){
				if(bl)sbSql.append(",");
				
				Entry<String, Object> enty = iparam.next();
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
			//设置Where条件
			if(!StringUtils.isEmpty(_where)){
				sbSql.append(" WHERE "+_where);
			}
		}else{
			//参数为空则，清空所有的SQL
			sbSql.setLength(0);
		}
		String sqlContent = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}

	/**
	 * 判断是否存在,不为null则是存在
	 * @param map
	 */
	public String existSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		@SuppressWarnings("unchecked")
		Map<String,Object> mparam = (Map<String,Object>)params.get("param");
		boolean bl = false;
		sbSql.append("SELECT count(*) FROM xbdun_depart WHERE ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = mparam.entrySet().iterator();
			while(iparam.hasNext()){
				
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
		}
		
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String queryPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int currentIndex = (Integer) params.get("pageNum");
		//显示条数
		int numPerPage = (Integer) params.get("numPerPage");

		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_depart");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY "+orderBy);
		}
		
		//分页
		if(numPerPage>0){
			sbSql.append(" LIMIT "+currentIndex+","+numPerPage);
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 查询分页查询总条数
	 * @param params
	 * @return
	 */
	public String queryPagerCountSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		
		sbSql.append("SELECT COUNT(1) ");
		sbSql.append(" From xbdun_depart Where 1=1 ");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" AND ");
			sbSql.append(where);
		}
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
		
	}
	//生成查询分页where条件
	private String constWhere(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" and ");}
				
				sbSql.append(enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
}
